package com.niuwa.p2p.action.borrowinfomg.enterpriseinfomg;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.core.utils.DictConstant.ChlAuditAction;
import com.niuwa.p2p.core.utils.DictUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.entity.enterprise.CorpBorrowHistoryVO;
import com.niuwa.p2p.entity.enterprise.EnterpriseBlackVO;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.IEnterpriseinfoMgService;
import com.niuwa.p2p.service.channel.IChannelCustomerService;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.BorrowCorpVo;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.CorpBasicInfoVo;
import com.niuwa.p2p.vo.channel.AuditFrom;
import com.niuwa.p2p.vo.channel.ChannelAuditLogVo;

/**
 * 借款企业action类
 * @author Administrator
 *
 */
public class EnterpriseInfoMgAction extends EnterpriseCommonAction{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private IEnterpriseinfoMgService enterpriseinfoMgService;
    
    @Autowired
	private IChannelCustomerService channelCustomerService;
    
	private AuditFrom auditFrom;
		   
	public AuditFrom getAuditFrom() {
		return auditFrom;
	}

	public void setAuditFrom(AuditFrom auditFrom) {
		this.auditFrom = auditFrom;
	}

	/**
	 * 跳转到企业信息列表
	 * 
	 * @return 企业信息列表视图
	 * 
	 */	
	@Action("gotoBorrowCorpInfoList")
	public String gotoBorrowCorpInfoList() {
		return "borrowEnterpriseInfoList";
	}
	
	/**
	 * 借款企业列表分页查询
	 * @return
	 */
 	@Action("listBorrowCorp")
 	public String BorrowCorpList() { 	
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<BorrowCorpVo>  pagination =  enterpriseinfoMgService.search(paramMap);
		// 设置返回		
		setObjectReSult(pagination);
		return NONE;
 	}
 	
	/**
	 * 跳转到审批操作记录
	 * 
	 * @return 审批操作记录视图
	 * 
	 */	
	@Action("gotoAuditOperateRecordList")
	public String gotoAuditOperateRecordList() {
		return "auditOperateRecordList";
	}
 	
	/**
	 * 审批操作记录列表分页查询
	 * @return
	 */
	@Action("listAuditOperateRecord")
	public String listAuditOperateRecord() {
		// 获取Grid条件选择参数
		Map<String, Object> paramMap = getParamMap();
		paramMap.put("channelCode", paramMap.get("coprCode"));
		// 执行查询操作
		PageBean<ChannelAuditLogVo>  pagination =  channelCustomerService.listAuditOperateRecord(paramMap);
		// 设置返回		
		setObjectReSult(pagination);
		return NONE;
	}
 	
	/**
	 * 删除
	 * @throws Exception 
	 */	
	@Action("deleteBorrowCopr")
	public String deleleteBorrowCopr() throws Exception {
		JsonResult result = new JsonResult();		
		enterpriseinfoMgService.deleteBorrowCopr(this.getCorpCode(), SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 提交审核
	 * @throws Exception 
	 */	
	@Action("toAuditBorrowCopr")
	public String toAuditBorrowCopr() throws Exception {
		JsonResult result = new JsonResult();		
		enterpriseinfoMgService.updateToAuditBorrowCopr(this.getCorpCode(), SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
			
	/**
	 * 审核
	 * @throws Exception 
	 */	
	@Action("auditBorrowCopr")
	public String auditBorrowCopr() throws Exception {
		JsonResult result = new JsonResult();
		String errorMsg = toAuditChannelFiledCheck();
		if(!StringUtil.isEmpty(errorMsg)) {
			result.setSuccess(false);
			result.setMsg(errorMsg);
			this.setResult(result);
			return NONE;
		}
		enterpriseinfoMgService.updateAuditBorrowCopr(this.getCorpCode(), auditFrom, SessionUtil.getSessionUser().getId());
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;	
	}
	
	/**
	 * 启用
	 * @throws Exception 
	 */	
	@Action("enableBorrowCorp")
	public String enableBorrowCorp() throws Exception {
		JsonResult result = new JsonResult();		
		enterpriseinfoMgService.updateEnableBorrowCorp(this.getCorpCode(), SessionUtil.getSessionUser().getId());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 添加渠道
	 */	
    @Action("addBorrowCorp")
    public String addBorrowCorp() {
    	this.setCoprOperateFlg(AppConstants.OPER_ADD);
        return "borrowCorpTab";
    }
	
	/**
	 * 察看渠道
	 */	
	@Action("viewBorrowCorp")
	public String viewBorrowCorp() {
		this.setCoprOperateFlg(AppConstants.OPER_VIEW);
		return "borrowCorpTab";
	}
	
	/**
	 * 编辑渠道
	 */	
	@Action("editBorrowCorp")
	public String editBorrowCorp() {
		this.setCoprOperateFlg(AppConstants.OPER_EDIT);
		return "borrowCorpTab";
	}	
    
	/**
	 * 加载基本信息
	 */	
    @Action("loadBorrowCorpBaseInfo")
    public String loadBorrowCorpBaseInfo() {
    	loadCorpBasicInfoVo();
        return "borrowCorpBaseInfo";
    }
    
	/**
	 * 加载信息核验
	 */	
    @Action("loadBorrowCorpInfoCheck")
    public String loadBorrowCorpInfoCheck() {
    	loadCorpBasicInfoVo();
        return "borrowCorpInfoCheck";
    }
    
    /**
	 * 加载借款记录
	 */	
    @Action("loadBorrowCorpBorrowRecord")
    public String loadBorrowCorpBorrowRecord() {
    	loadCorpBasicInfoVo();
        return "borrowCorpBorrowRecord";
    }
    
	/**
	 *  加载文件上传
	 */	
    @Action("loadBorrowCorpFileUpload")
    public String loadBorrowCorpFileUpload() {
    	loadCorpBasicInfoVo();
        return "borrowCorpFileUpload";
    }    
	/**
	 *  加载企业信息
	 */	
	private void loadCorpBasicInfoVo(){
    	if(!StringUtil.isEmpty(this.getCorpCode())){
    		CorpBasicInfoVo corpBasicInfo = enterpriseinfoMgService.getCorpBasicInfo(this.getCorpCode());
    		this.setCorpBasicInfoVo(corpBasicInfo); 
    	}   
	}
	
	/**
	 * 提交审核信息check
	 * 
	 * @return check信息(无错误:"")
	 */
	private String toAuditChannelFiledCheck(){
		
		final String CHL_AUDIT_ACTION = "CHL_AUDIT_ACTION";
		String operateType = auditFrom.getOperateType();
		// 审核类型check 
		if(StringUtil.isEmpty(operateType)) {
			return this.getText("EnterpriseInfoMgAction.operateType.RequiredString.message");
		}
		if(StringUtil.isEmpty(DictUtil.getDictNameByTypeAndCode(CHL_AUDIT_ACTION, operateType))) {
			return this.getText("EnterpriseInfoMgAction.operateType.TypeError.message");
		}
		// 审核意见check 
		if(ChlAuditAction.RETURN.getCode().equals(auditFrom.getOperateType())
				|| ChlAuditAction.REJECT.getCode().equals(auditFrom.getOperateType())) {
			if(StringUtil.isEmpty(auditFrom.getRemark())) {
				return this.getText("EnterpriseInfoMgAction.remark.RequiredString.message");
			}
			if(auditFrom.getRemark().length() > 100) {
				return this.getText("EnterpriseInfoMgAction.remark.StringLength.message");
			}	
		}
				
		return "";
	}
 	
	/**
	 * 添加黑名单
	 * @throws Exception 
	 */	
	@Action("addBlackList")
	public String addBlackList() throws Exception {
		JsonResult result = new JsonResult();	
		EnterpriseBlackVO enterpriseBlackVO = new EnterpriseBlackVO();
		Corporation corporation = getCorpBasicInfoVo().getCorporation();
		enterpriseBlackVO.setCorpCode(corporation.getCorpCode());
		enterpriseBlackVO.setCorpName(corporation.getCorpName());
		enterpriseBlackVO.setIsBlack("1");
		enterpriseinfoMgService.addNewBlackEnterprise(enterpriseBlackVO);
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	/**
	 * 添加涉嫌欺诈名单
	 * @throws Exception 
	 */	
	@Action("addCheatList")
	public String addCheatList() throws Exception {
		JsonResult result = new JsonResult();	
		EnterpriseBlackVO enterpriseBlackVO = new EnterpriseBlackVO();
		Corporation corporation = getCorpBasicInfoVo().getCorporation();
		enterpriseBlackVO.setCorpCode(corporation.getCorpCode());
		enterpriseBlackVO.setCorpName(corporation.getCorpName());
		enterpriseBlackVO.setIsCheat("1");
		enterpriseinfoMgService.addNewBlackEnterprise(enterpriseBlackVO);
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	
	
    @Action("loadBorrowRecord")
    public String loadBorrowRecord() {
        return "borrowCorpBorrowRecord";
    }  
	/**
	 * 
	 * @throws Exception 
	 */	
	@Action("getBorrowHistory")
	public String getBorrowHistorys() throws Exception {
		JsonResult result = new JsonResult();	
		Map<String, Object> paramMap = getParamMap();
		String corpCode = this.getCorpCode();
		if(StringUtil.isEmpty(corpCode)) {
			setErrorInfo(result, "尚未生成企业编码!");
			return NONE;
		}
		CorpBasicInfoVo corpBasicInfoVo = enterpriseinfoMgService.getCorpBasicInfo(corpCode);
		if(corpBasicInfoVo==null){ 
			setErrorInfo(result, "尚无企业信息!");
			return NONE;
		}
		String corpName = corpBasicInfoVo.getCorporation().getCorpName();
		if(StringUtil.isEmpty(corpName)){
			setErrorInfo(result, "企业名称为空!");
			return NONE;
		}
		paramMap.put("corpName", corpName);
		PageBean<CorpBorrowHistoryVO> page =  enterpriseinfoMgService.getBorrowHistorys(paramMap);//accountFlowManagerService.searchNwAccountFlow(paramMap);
		setObjectReSult(page);
		return NONE;
	}
	public void setErrorInfo(JsonResult result,String msg){
		result.setSuccess(false);
		result.setMsg(msg);
	}
}	
